﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product.Base;
using ERP_RORZE_PERMISSION.Base;

namespace ERP_RORZE_PRODUCT.Product.MO
{
    public partial class ctrlMOMaterial : DevExpress.XtraEditors.XtraUserControl
    {
        ClsMODAO clsMODAO = new ClsMODAO();
        private LogUtils log;
        DataTable dt;
        ClsMOMaterialTable b = new ClsMOMaterialTable();

        public ctrlMOMaterial()
        {
            InitializeComponent();
            log = new LogUtils();
        }
        void Reload(object sender, EventArgs e)
        {
            dt = clsMODAO.GetAllMOMaterial();
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
        }
        private void btnReload_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            b.ID = int.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString());
            clsMODAO.DeleteMOMaterial(b);

            //write Log
            log.Table = LogConstant.Table.Product.MOMaterial;
            log.Task = LogConstant.Task.Delete;
            log.Description = "MONo: " + b.MONo + "; PartID: " + b.PartID;
            log.WriteLog();

            Reload(sender, e);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frm_MOMaterialAdd frmAdd = new frm_MOMaterialAdd();
            frmAdd.Reload += new EventHandler(Reload);
            frmAdd.Show();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                frm_MOMaterialEdit frmEdit = new frm_MOMaterialEdit();
                frmEdit.Reload += new EventHandler(Reload);
                frmEdit.ID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString();
                frmEdit.CutSize = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "CutSize").ToString();
                frmEdit.Finishdate = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Finishdate").ToString();
                frmEdit.Finished = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Finished").ToString();
                frmEdit.lenght = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "lenght").ToString();
                frmEdit.MaterialID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MaterialID").ToString();
                frmEdit.MaterialPrice = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MaterialPrice").ToString();
                frmEdit.MaterialType = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MaterialType").ToString();
                frmEdit.MONo = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MONo").ToString();
                frmEdit.Note = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Note").ToString();
                frmEdit.PartID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "PartID").ToString();
                frmEdit.Price = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Price").ToString();
                frmEdit.Shape = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Shape").ToString();
                frmEdit.Thickness = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Thickness").ToString();
                frmEdit.width = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "width").ToString();
                frmEdit.ShowDialog();
            }
            catch
            {
            }
        }

        private void ctrlMOMaterial_Load(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            btnAdd_Click(sender, e);
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            btnEdit_Click(sender, e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            btnDelete_Click(sender, e);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            clsConstantsFuction.ExportToExcel(gridView1, 1, true);
        }
    }
}
